﻿using Corina.Datamodel.Entities;
using Corina.Enumerations;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace Corina.DataContext.Service
{
    public class WeburlTopService : ServiceBase
    {
        public List<WeburlTopInfo> GetFetchedTopurl(string connectionString, int roleOrLevelId, PlatformTypeValue platform, AccountTypeValue accountType)
        {
            List<SqlParameter> paras = new List<SqlParameter>();
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT * FROM t_weburl_top AS twt ");
            builder.Append("WHERE 1=1 AND twt.PlatformType =@Platform ");
            builder.Append("AND ( twt.ShowToAll = 1 OR twt.Id IN (SELECT twtp.TopurlId FROM t_weburl_top_permission AS twtp WHERE twtp.RoleOrLevelId = @RoleOrLevel AND twtp.AccountType = @AccountType)) ORDER BY twt.[Index]");

            paras.Add(new SqlParameter("@Platform", (int)platform));
            paras.Add(new SqlParameter("@RoleOrLevel", roleOrLevelId));
            paras.Add(new SqlParameter("@AccountType", (int)accountType));

            return DbOperator.GetEntities<WeburlTopInfo>(connectionString, builder.ToString(), paras.ToArray());
        }

        public List<WeburlTopInfo> GetAllTopurl(string connectionString, string title, params PlatformTypeValue[] platforms)
        {
            List<SqlParameter> paras = new List<SqlParameter>();
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT * FROM t_weburl_top AS twt ");
            builder.Append("WHERE 1=1 ");
            if (platforms.Length > 0)
            {
                builder.Append(" AND twt.PlatformType =@Platform ");
                paras.Add(new SqlParameter("@Platform", (int)platforms[0]));
            }
            builder.Append("AND twt.Title LIKE @Title");
            paras.Add(new SqlParameter("@Title", string.Format("%{0}%", title)));

            return DbOperator.GetEntities<WeburlTopInfo>(connectionString, builder.ToString(), paras.ToArray());
        }

        public bool Update(string connectionString,WeburlTopInfo entity,EntityOperation operation)
        {
            return DbOperator.UpdateEntity(connectionString, entity, operation);
        }
    }
}
